package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.NumberFormatUtils;
import com.yidiandao.business.utils.UIUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 发现搜索结果
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 10:49
 */
public class DiscoverySearchArticleProvider extends ItemViewProvider<Article, DiscoverySearchArticleProvider.ViewHolder> {

    private Activity mActivity;

    public DiscoverySearchArticleProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_discovery_search_article, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final Article article) {

        holder.tvTitle.setText(article.getArticleTitle());
        holder.tvContent.setText(article.getArticleDesc());

        String commentCount = NumberFormatUtils.getLargeFormatValue(article.getCommentCount());
        String watchCount = NumberFormatUtils.getLargeFormatValue(article.getWatchCount());
        String likeCount = NumberFormatUtils.getLargeFormatValue(article.getVoteCount());

        holder.tvCommentCount.setText(commentCount);
        holder.tvReadCount.setText(UIUtils.getString(R.string.people_read, watchCount));
        holder.tvLikeCount.setText(likeCount);
        holder.tvLikeCount.setSelected(article.getHasVote() == 1);

        ImageLoader.loadImage(mActivity, holder.ivImage, article.getArticleThumb());

        // 动态隐藏 右边的模块
        holder.tvCommentCount.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (holder.tvCommentCount.getRight() > holder.tvReadCount.getLeft() + 30) {
                    holder.tvReadCount.setVisibility(View.GONE);
                } else {
                    holder.tvReadCount.setVisibility(View.VISIBLE);
                }
                holder.tvCommentCount.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UrlActivity.actionStartForArticle(mActivity, article.getArticleId());
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivImage;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvLikeCount;
        private TextView tvCommentCount;
        private TextView tvReadCount;

        public ViewHolder(View itemView) {
            super(itemView);

            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            tvLikeCount = (TextView) itemView.findViewById(R.id.iv_like_count_icon);
            tvCommentCount = (TextView) itemView.findViewById(R.id.iv_comment_count_icon);
            tvReadCount = (TextView) itemView.findViewById(R.id.tv_read_count);
        }
    }
}
